
#!/usr/bin/env python3
"""
单张影像预测脚本
python predict.py --model svm.pkl --image test_ct.png
"""
import argparse
import joblib
from utils import preprocess_image, extract_features

def main():
    parser = argparse.ArgumentParser(description='单张CT影像肺结节良恶性预测')
    parser.add_argument('--model', required=True, help='已训练SVM模型路径')
    parser.add_argument('--image', required=True, help='待预测影像路径')
    args = parser.parse_args()

    clf = joblib.load(args.model)
    img = preprocess_image(args.image)
    feat = extract_features(img).reshape(1, -1)

    proba = clf.predict_proba(feat)[0]
    pred  = clf.predict(feat)[0]

    label_name = '恶性' if pred == 1 else '良性'
    confidence = proba[pred]

    print(f"预测结果: {label_name}  (置信度: {confidence:.2%})")

if __name__ == '__main__':
    main()
